package com.github.linkoog.model_designer.ide.editer.common;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ColumnVo extends Column {

    private String comment;
    private Boolean primaryKey;
    private DataType dataType;
    private String dialectType;
    private Integer length;
    private Integer radix;
    private Boolean notNull;
    private String defaultValue;
    private String mockRule;

    public static final List<String> fields = Arrays.asList("comment", "primaryKey","dataType","dialectType","length","radix","notNull","defaultValue","mockRule");
    public static final Map<String,String> defaultAttributeMap;
    static {
        defaultAttributeMap  = new LinkedHashMap();
        defaultAttributeMap.put("comment", "");
        defaultAttributeMap.put("primaryKey", "false");
        defaultAttributeMap.put("dataType", "varchar");
        defaultAttributeMap.put("dialectType", "");
        defaultAttributeMap.put("length", "32");
        defaultAttributeMap.put("radix", "0");
        defaultAttributeMap.put("notNull", "false");
        defaultAttributeMap.put("defaultValue", "");
        defaultAttributeMap.put("mockRule", "");
    }

    public void setField(String name, String value){
        if (StringUtils.isBlank(name)){
            return;
        }

        switch (name){
            case "comment":
                comment = StringUtils.isNotBlank(value)? value : "";
                return;
            case "primaryKey":
                primaryKey = parseBool(value,false);
                return;
            case "dataType":
                if (StringUtils.isBlank(value)){
                    dataType = DataType.VARCHAR;
                } else {
                    DataType tmp = DataType.valueOfName(value);
                    dataType = tmp != null? tmp : DataType.VARCHAR;
                }
                return;
            case "dialectType":
                dialectType = StringUtils.isNotBlank(value)? value : "";
                return;
            case "length":
                length = parseInteger(value,32);
                return;
            case "radix":
                radix = parseInteger(value,0);
                return;
            case "notNull":
                notNull = parseBool(value,false);
                return;
            case "defaultValue":
                defaultValue = StringUtils.isNotBlank(value)? value : "";
                return;
            case "mockRule":
                mockRule = StringUtils.isNotBlank(value)? value : "";
                return;
        }
    }

    public String getField(String name){
        if (StringUtils.isBlank(name)){
            return null;
        }

        switch (name){
            case "comment":
                return StringUtils.isNotBlank(comment)? comment : "";
            case "primaryKey":
                return (primaryKey != null && primaryKey) ? "true" : "false";
            case "dataType":
                if (dataType == null){
                    return DataType.VARCHAR.getName();
                }
                return dataType.getName();
            case "dialectType":
                return StringUtils.isNotBlank(dialectType)? dialectType : "";
            case "length":
                return length != null? length.toString() : "0";
            case "radix":
                return radix != null? radix.toString() : "0";
            case "notNull":
                return (notNull != null && notNull) ? "true" : "false";
            case "defaultValue":
                return StringUtils.isNotBlank(defaultValue)? defaultValue : "";
            case "mockRule":
                return StringUtils.isNotBlank(mockRule)? mockRule : "";
        }

        return null;
    }


    private int parseInteger(String value,int defaultVal){
        if (StringUtils.isBlank(value)){
            return defaultVal;
        }

        try {
            return Integer.valueOf(value);
        }catch (Exception e){
        }

        return defaultVal;
    }

    private boolean parseBool(String value, boolean defaultVal){
        if (StringUtils.isBlank(value)){
            return defaultVal;
        }

        if ("true".equals(value)){
            return true;
        } else if ("false".equals(value)){
            return false;
        }

        return defaultVal;
    }


    public static ColumnVo newStringColumnVo(){
        ColumnVo columnVo = new ColumnVo();
        columnVo.setName("字段 1");
        columnVo.setComment("");
        columnVo.setPrimaryKey(false);
        columnVo.setDataType(DataType.VARCHAR);
        columnVo.setLength(255);
        columnVo.setRadix(0);
        columnVo.setNotNull(false);
        columnVo.setDefaultValue("");
        columnVo.setMockRule("");
        return columnVo;
    }

    public static ColumnVo copy(ColumnVo src){
        if (src == null){
            return null;
        }

        ColumnVo columnVo = new ColumnVo();
        columnVo.setName(src.name);
        columnVo.setComment(src.comment);
        columnVo.setPrimaryKey(src.primaryKey);
        columnVo.setDataType(src.dataType);
        columnVo.setDialectType(src.dialectType);
        columnVo.setLength(src.length);
        columnVo.setRadix(src.radix);
        columnVo.setNotNull(src.notNull);
        columnVo.setDefaultValue(src.defaultValue);
        columnVo.setMockRule(src.mockRule);
        return columnVo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Boolean getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(Boolean primaryKey) {
        this.primaryKey = primaryKey;
    }

    public DataType getDataType() {
        return dataType;
    }

    public void setDataType(DataType dataType) {
        this.dataType = dataType;
    }

    public String getDialectType() {
        return dialectType;
    }

    public void setDialectType(String dialectType) {
        this.dialectType = dialectType;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public Integer getRadix() {
        return radix;
    }

    public void setRadix(Integer radix) {
        this.radix = radix;
    }

    public Boolean getNotNull() {
        return notNull;
    }

    public void setNotNull(Boolean notNull) {
        this.notNull = notNull;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public String getMockRule() {
        return mockRule;
    }

    public void setMockRule(String mockRule) {
        this.mockRule = mockRule;
    }

    @Override
    public ColumnVo getColumnVo() {
        return this;
    }

    @Override
    public boolean isColumnVo() {
        return true;
    }

    @Override
    public String toString() {
        return "ColumnSelf{" +
                "name='" + name + '\'' +
                ", comment='" + comment + '\'' +
                ", primaryKey=" + primaryKey +
                ", dataType=" + dataType +
                ", dialectType='" + dialectType + '\'' +
                ", length=" + length +
                ", radix=" + radix +
                ", notNull=" + notNull +
                ", defaultValue='" + defaultValue + '\'' +
                ", mockRule='" + mockRule + '\'' +
                '}';
    }

}
